package com.xm.share.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xm.share.common.utils.DateUtils;
import com.xm.share.mapper.CommentMapper;
import com.xm.share.mapper.PictureFileMapper;
import com.xm.share.mapper.PictureshareMapper;
import com.xm.share.mapper.UserInfoMapper;
import com.xm.share.model.PictureFile;
import com.xm.share.model.Pictureshare;
import com.xm.share.model.vo.CommentDto;
import com.xm.share.model.vo.NoticeData;
import com.xm.share.model.vo.PictureShareDetailDto;
import com.xm.share.model.vo.PictureShareRequest;
import com.xm.share.service.PictureShareService;

@Service
public class PictureShareServiceImpl implements PictureShareService {

	@Autowired
	private PictureshareMapper pictureshareMapper;

	@Autowired
	private PictureFileMapper pictureFileMapper;

	@Autowired
	private CommentMapper commentMapper;

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Override
	public List<PictureShareDetailDto> getDetailList(int pageNo, int pageSize) {
		if (pageNo <= 0) {
			pageNo = 1;
		}
		int begin = (pageNo - 1) * pageSize;
		int end = pageNo * pageSize;
		List<Pictureshare> pictureshares = pictureshareMapper.findPagedPicture(begin, end);
		int size = 0;
		if (pictureshares != null) {
			size = pictureshares.size();
		}
		List<PictureShareDetailDto> pictureShareDetailDtos = new ArrayList<>(size);
		for (Pictureshare pictureshare : pictureshares) {
			PictureShareDetailDto pictureShareDetailDto = new PictureShareDetailDto();

			pictureShareDetailDto.setId(pictureshare.getId());
			pictureShareDetailDto.setRemark(pictureshare.getRemark());
			pictureShareDetailDto.setUserId(pictureshare.getUserId());
			pictureShareDetailDto.setCreatedOn(DateUtils.DATETIME.format(pictureshare.getCreatedOn()));

			String username = userInfoMapper.findNameByUserId(pictureshare.getUserId());

			pictureShareDetailDto.setUserName(username);

			List<Long> fileURLList = pictureFileMapper.findURLByShareId(pictureshare.getId());
			pictureShareDetailDto.setFileURLList(fileURLList);

			List<CommentDto> commentDtoList = commentMapper.findCommentDtosBySharedId(pictureshare.getId());
			pictureShareDetailDto.setCommentDtoList(commentDtoList);

			pictureShareDetailDtos.add(pictureShareDetailDto);
		}
		return pictureShareDetailDtos;
	}

	@Override
	public Pictureshare findById(Long id) {
		return pictureshareMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deletePictureShare(Long id) {
		pictureshareMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<PictureShareDetailDto> findPictureShare(String condition, String order) {
		List<Pictureshare> pictureshares = pictureshareMapper.findPictureShare(condition, order);
		List<PictureShareDetailDto> pictureShareDetailDtos = new ArrayList<>();
		if (pictureshares != null) {

			for (Pictureshare pictureshare : pictureshares) {
				PictureShareDetailDto pictureShareDetailDto = new PictureShareDetailDto();

				pictureShareDetailDto.setId(pictureshare.getId());
				pictureShareDetailDto.setRemark(pictureshare.getRemark());
				pictureShareDetailDto.setUserId(pictureshare.getUserId());
				pictureShareDetailDto.setCreatedOn(DateUtils.DATETIME.format(pictureshare.getCreatedOn()));

				String username = userInfoMapper.findNameByUserId(pictureshare.getUserId());

				pictureShareDetailDto.setUserName(username);

				List<Long> fileURLList = pictureFileMapper.findURLByShareId(pictureshare.getId());
				pictureShareDetailDto.setFileURLList(fileURLList);

				List<CommentDto> commentDtoList = commentMapper.findCommentDtosBySharedId(pictureshare.getId());
				pictureShareDetailDto.setCommentDtoList(commentDtoList);

				pictureShareDetailDtos.add(pictureShareDetailDto);
			}
		}
		return pictureShareDetailDtos;
	}

	@Override
	public void addPictureShare(PictureShareRequest pictureShareRequest) {
		Pictureshare pictureShare = pictureShareRequest.getPictureShare();
		pictureshareMapper.insert(pictureShare);
		List<PictureFile> pictureFileList = pictureShareRequest.getPictureFileList();
		if(pictureFileList.size() > 0){
			for (PictureFile pictureFile : pictureFileList) {
				pictureFile.setPictureShareId(pictureShare.getId());
				pictureFile.setCreatedOn(new Date());
				pictureFileMapper.insert(pictureFile);
			}
		}
	}

	@Override
	public void makeTop(Long id, Long userId) {
		Pictureshare pictureshare = new Pictureshare();
		pictureshare.setId(id);
		pictureshare.setTop(true);
		pictureshare.setTopTime(new Date());
		pictureshare.setTopUser(userId);
		pictureshare.setRead(false);
		pictureshareMapper.updateByPrimaryKeySelective(pictureshare);
	}

	@Override
	public PictureShareDetailDto getDetail(Long pictureShareId) {
		PictureShareDetailDto pictureShareDetailDto = null;
		Pictureshare pictureshare = pictureshareMapper.selectByPrimaryKey(pictureShareId);
		if (pictureshare != null) {
			pictureShareDetailDto = new PictureShareDetailDto();
			pictureShareDetailDto.setUserId(pictureshare.getUserId());
			Long userId = pictureshare.getUserId();
			String name = userInfoMapper.findNameByUserId(userId);
			pictureShareDetailDto.setUserName(name);
			pictureShareDetailDto.setId(pictureshare.getId());
			pictureShareDetailDto.setRemark(pictureshare.getRemark());
			pictureShareDetailDto.setUserId(pictureshare.getUserId());
			pictureShareDetailDto.setCreatedOn(DateUtils.DATETIME.format(pictureshare.getCreatedOn()));

			List<Long> fileURLList = pictureFileMapper.findURLByShareId(pictureshare.getId());
			if (fileURLList != null) {
				pictureShareDetailDto.setFileURLList(fileURLList);
			}
			List<CommentDto> commentDtoList = commentMapper.findCommentDtosBySharedId(pictureshare.getId());
			if (commentDtoList != null) {
				pictureShareDetailDto.setCommentDtoList(commentDtoList);
			}
		}
		return pictureShareDetailDto;
	}

	@Override
	public List<PictureShareDetailDto> getDetailList(long userId) {
		List<PictureShareDetailDto> pictureShareDetailDtos = new ArrayList<>();
		List<Pictureshare> pictureshares = pictureshareMapper.findByUserId(userId);
		if (pictureshares != null && pictureshares.size() > 0) {
			for (Pictureshare pictureshare : pictureshares) {
				PictureShareDetailDto pictureShareDetailDto = new PictureShareDetailDto();

				pictureShareDetailDto.setId(pictureshare.getId());
				pictureShareDetailDto.setRemark(pictureshare.getRemark());
				pictureShareDetailDto.setUserId(pictureshare.getUserId());
				pictureShareDetailDto.setCreatedOn(DateUtils.DATETIME.format(pictureshare.getCreatedOn()));

				String username = userInfoMapper.findNameByUserId(pictureshare.getUserId());

				pictureShareDetailDto.setUserName(username);

				List<Long> fileURLList = pictureFileMapper.findURLByShareId(pictureshare.getId());
				pictureShareDetailDto.setFileURLList(fileURLList);

				List<CommentDto> commentDtoList = commentMapper.findCommentDtosBySharedId(pictureshare.getId());
				pictureShareDetailDto.setCommentDtoList(commentDtoList);

				pictureShareDetailDtos.add(pictureShareDetailDto);
			}
		}
		return pictureShareDetailDtos;
	}

	@Override
	public List<NoticeData> queryTopMsg() {
		return pictureshareMapper.queryTopMsg();
	}

	@Override
	public NoticeData queryTopMsgByuserId(String userId) {
		return pictureshareMapper.queryTopMsgByuserId(userId);
	}

	@Override
	public void setRead(long id) {
		pictureshareMapper.setRead(id);
	}

	@Override
	public void deleteTop(Long id, Long userId) {
		Pictureshare pictureshare = new Pictureshare();
		pictureshare.setId(id);
		pictureshare.setTop(false);
		pictureshare.setTopTime(new Date());
		pictureshare.setTopUser(userId);
		pictureshare.setRead(true);
		pictureshareMapper.updateByPrimaryKeySelective(pictureshare);
	}
}
